package com.jic.market.impl.rest.discountamount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.coupon.CouponConfig;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discountamount.model.PromotionAmountModel;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import com.jic.market.mapper.coupon.CouponConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 红包
 * @author yinhaijun
 * @date: 2020/4/10
 */
@Service("RedAmountProcessor")
public class RedAmountProcessor extends AmountProcessor {

    @Autowired
    private CouponConfigMapper couponConfigMapper;


    @Override
    protected Boolean isCondition(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, String thirdUserId) {
        List<String> couponCodeList = promotionAmountModel.getCouponCodeList();
        if(null == couponCodeList || couponCodeList.isEmpty()){
            return false;
        }

        CouponConfig couponConfig = couponConfigMapper.queryByPromotionId(promotionAmountModel.getPromotionId());
        // 改变红包的面值，为多张优惠劵之和
        BigDecimal allCouponAmount = Money.multiply(couponConfig.getCouponAmount(), new BigDecimal(couponCodeList.size()));
        couponConfig.setCouponAmount(allCouponAmount);

        // 参数错误
        if(null == couponConfig || null == couponConfig.getUseTotalAmountLimit()){
            return false;
        }
        // 未达到使用优惠劵的金额
        if(goodsPo.getTotalAmount().compareTo(couponConfig.getUseTotalAmountLimit()) == -1){
            return false;
        }
        promotionAmountModel.setRuleData(couponConfig);
        return true;
    }

    @Override
    protected BigDecimal promotionDiscountTotalAmount(PromotionAmountModel promotionAmountModel, GoodsPo goodsPo, Object request) {
        CouponConfig couponConfig = (CouponConfig) promotionAmountModel.getRuleData();
        couponConfig.getCouponAmount();
        // 0是金额劵
        if(couponConfig.getCouponMode().equals(0)){
            return couponConfig.getCouponAmount();
        }else{
            // 1是折扣劵
            BigDecimal youhuizhekou = Money.subtract(new BigDecimal(1), couponConfig.getCouponAmount());
            return Money.multiply(goodsPo.getTotalAmount(), youhuizhekou);
        }
    }

}
